In this example, we will add custom layout into alert dialog by clicking on a Button. See the following example:
activity_main.xml
Add the following code in the activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="15dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_custom_alert_dialog"
android:layout_marginTop="10dp"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textColor="#000000"
android:text="Custom Alert Dialog"/>
<TextView
android:id="@+id/txt_display_info"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="#000000"/>
</LinearLayout>Create info_dialog.xml: res-> layout-> info_dialog.xml
info_dialog.xml
Add the following code in the info_dialog.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<EditText
android:id="@+id/edt_dia_first_name"
android:hint="Enter First Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>
<EditText
android:id="@+id/edt_dia_second_name"
android:hint="Enter Second Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>
<EditText
android:id="@+id/edt_dia_last_name"
android:hint="Enter Last Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/btn_dia_submit"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="Submit"
android:textAllCaps="false"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_dia_cancel"
android:layout_margin="5dp"
android:layout_weight="1"
android:textAllCaps="false"
android:text="Cancel"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
MainActivity.kt
Add the following code in the MainActivity.kt class.
package com.study.kotlinkatta
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.info_dialog.view.*
class MainActivity : AppCompatActivity() {
lateinit var btn_custom_alert_dialog: Button
lateinit var txt_display_info: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
txt_display_info = findViewById(R.id.txt_display_info)
btn_custom_alert_dialog = findViewById(R.id.btn_custom_alert_dialog)
btn_custom_alert_dialog.setOnClickListener {
fun_custom_alert_dialog()
}
}
private fun fun_custom_alert_dialog() {
val alert_DialogView = LayoutInflater.from(this).inflate(R.layout.info_dialog, null)
val mBuilder = AlertDialog.Builder(this)
.setView(alert_DialogView)
.setTitle("Info Dialog")
val alertDialog = mBuilder.show()
var edt_dia_first_name: EditText
var edt_dia_second_name: EditText
var edt_dia_last_name: EditText
var btn_dia_submit: Button
var btn_dia_cancel: Button
edt_dia_first_name = alert_DialogView.findViewById(R.id.edt_dia_first_name)
edt_dia_second_name = alert_DialogView.findViewById(R.id.edt_dia_second_name)
edt_dia_last_name = alert_DialogView.findViewById(R.id.edt_dia_last_name)
btn_dia_submit = alert_DialogView.findViewById(R.id.btn_dia_submit)
btn_dia_submit.setOnClickListener {
val str_first_name = alert_DialogView.edt_dia_first_name.text.toString()
val str_second_name = alert_DialogView.edt_dia_second_name.text.toString()
val str_last_name = alert_DialogView.edt_dia_last_name.text.toString()
if(str_first_name.equals("") || str_second_name.equals("") || str_last_name.equals("")){
Toast.makeText(this,"Please enter details.",Toast.LENGTH_SHORT).show()
}else {
alertDialog.dismiss()
txt_display_info.setText("Info:-" + "\nFirst Name:" + str_first_name + "\nSecond Name: " + str_second_name + "\nLast Name: " + str_last_name)
}
}
btn_dia_cancel = alert_DialogView.findViewById(R.id.btn_dia_cancel)
alert_DialogView.btn_dia_cancel.setOnClickListener {
alertDialog.dismiss()
}
}
}Output:
Comments
Post a Comment